package com.mstore.dao;

import java.util.List;

import com.mstore.pojo.Notice;

public class NoticeDao extends SuperDao {

	public void addNotices(Notice notice) {
		save(notice);
	}
	
	/**
	 * 查询企业通知
	 */
	public List<Object> getNoticesByEaid(Long eaId) {
		String sql = "from Notice where eaId = ?";
		return super.getObject(sql, new Object[] { eaId });
	}

	/**
	 * 根据类型查询企业通知条数
	 */
	public List<Object> getCountNoticesByType(Long eaId) {
		String sql = "select n.type, count(n.id) from Notice n where n.eaId = ? and n.isRead = 0 group by n.type";
		return super.getObject(sql, new Object[] { eaId });
	}
	
	/**
	 * 修改通知的状态
	 */
	public void modifyReadStatus(Long eaId, Integer type) {
		String separate = " where ";
		StringBuilder sb = new StringBuilder();
		sb.append("update ubcenter.b_notice n set n.is_read = 1");
		if (eaId != null) {
			sb.append(separate);
			sb.append("n.ea_id = ");
			sb.append(eaId);
			separate = " and ";
		}
		if (type != null) {
			sb.append(separate);
			sb.append("n.b_type = ");
			sb.append(type);
			separate = " and ";
		}
		
		if (" where ".equals(separate)) {
			sb.append(separate);
			sb.append("1 = 0");
		}else {
			sb.append(separate);
			sb.append("n.is_read = 0");
		}
		
		executeUpdate(sb.toString());
	}
}
